/*
 * Tartaruga.java
 *
 * Created on May 6, 2007, 10:42 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package labirinto;

import javax.swing.*;
import java.awt.*;

/**
 *
 * @author thiago
 */
public class Tartaruga extends Oponente
{
    private JLabel lbTartaruga;
    private int posX,posY,desvio;
    private boolean direcao; // true = direita
    private ImageIcon imgDir,imgEsq;
    
    
    /** Creates a new instance of Tartaruga */
    public Tartaruga(Jogo jogo)
    {
        super(jogo);
        this.lbTartaruga = new JLabel();
        this.lbTartaruga.setSize(32,32);
        this.lbTartaruga.setIcon(this.imgDir = new ImageIcon(this.getClass().getResource("/anishellcreeperD.gif")));
        this.imgEsq = new ImageIcon(this.getClass().getResource("/anishellcreeperE.gif"));
        this.posX = this.posY = this.desvio = 0;
        this.direcao = true;
    }
    
    public void setPosX(int x)
    {
        this.posX = x;
    }
    
    public void setPosY(int y)
    {
        this.posY = y;
    }
    
    public int getPosX()
    {
        return this.posX;
    }
    
    public int getPosY()
    {
        return this.posY;
    }
    
    public void vive()
    {
        this.jogo.getPaCenario().add(this.lbTartaruga);
        this.lbTartaruga.setLocation(this.posX,this.posY);
        this.vivo = true;
        this.start();
    }
    
    public void run()
    {
        try
        {
            Thread.sleep(1000);
        }
        catch(Exception e)
        {}
        Thing[][] mapa = this.jogo.getMapa();
        int offX=0,offY=0;
        while(this.vivo)
        {
            if (this.posX < 640 && this.posX > 0)
            {
                Thing[][] mo = this.jogo.getMapaOponentes();
                mo[offX][offY] = null;
                int vel = 0;
                if (this.direcao)
                {
                    vel = 2;
                    offX = (int)(this.posX + vel + 32) / 32;
                    this.lbTartaruga.setIcon(this.imgDir);
                }
                else
                {
                    vel = -2;
                    offX = (int)(this.posX + vel) / 32;
                    this.lbTartaruga.setIcon(this.imgEsq);
                }
                offY = (int)this.posY / 32;
                
                if (!mapa[offX][offY].getIsConcrete())
                {
                    this.posX += vel;
                    mo[offX][offY] = this.myThing;
                }
                else
                {
                    this.direcao = !this.direcao;
                }
                if (offY < 14)
                {
                    if (!mapa[offX][offY+1].getIsConcrete())
                    {
                        this.direcao = !this.direcao;
                    }
                }
            }
            else
            {
                this.direcao = !this.direcao;
            }
            this.lbTartaruga.setLocation(this.posX,this.posY);
            try
            {
                Thread.sleep(30);
            }
            catch(Exception e)
            {}
            
        }
    }
    
    public void morre()
    {
        this.vivo = false;
        try
        {
            Thread.sleep(35);
        }
        catch(Exception e)
        {}
        this.jogo.getPaCenario().remove(this.lbTartaruga);
        // this.jogo.getPaCenario().repaint();
    }
    
}
